import React from "react";
import { Modal } from "antd";
import { Props } from "@/types/type";
import { useTranslation } from "react-i18next";
import MyForm from "./form";

const AddData: React.FC<Props> = (props: Props) => {
  const { t } = useTranslation();
  const { open, close, editok, addok, dialog, data, tags } = props;

  //表单处理
  const handleForm = (values: any) => {
    let urlres = undefined;
    if (values.url !== undefined) {
      urlres =
        values.url.length === 0
          ? undefined
          : values.url[0].response !== undefined
          ? values.url[0].response.imgname
          : values.url[0].name;
    }
    const formdata = {
      ...values,
      id: data ? data.id : undefined,
      url: urlres,
    };
    if (dialog === "Edit") {
      editok(formdata);
    } else if (dialog === "Add") {
      addok(formdata);
    }
  };
  return (
    // 修改、编辑数据弹窗
    <Modal
      title={dialog === "Edit" ? t("form.editdata") : t("addj")}
      open={open}
      footer={null}
      centered={true}
      forceRender
      onCancel={close}
    >
      <MyForm data={data} tags={tags} dialog={dialog} handleForm={handleForm} />
    </Modal>
  );
};

export default AddData;
